package E5.Assign3;
import E5.Assign1.MyBC;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client1 {
    public static void main(String[] args) {
        Scanner inn = new Scanner(System.in);
        Socket mysocket;
        DataInputStream in = null;
        DataOutputStream out = null;
        try{
            mysocket = new Socket("127.0.0.1",2010);
            in = new DataInputStream(mysocket.getInputStream());
            out = new DataOutputStream(mysocket.getOutputStream());
            KeyGenerator kg=KeyGenerator.getInstance("DESede");
            kg.init(168);
            SecretKey k=kg.generateKey( );
            byte[ ] kb=k.getEncoded( );
            out.writeUTF(kb.length+"");
            for(int i=0; i<kb.length; i++){
                System.out.print(kb[i]+" ");
                out.writeUTF(kb[i]+"");
            }
            System.out.println("请输入中缀表达式：");
            String infix = inn.nextLine();
            MyBC myBC = new MyBC();
            String suffix = myBC.infixToSuffix(infix);
            Cipher cp=Cipher.getInstance("DESede");
            cp.init(Cipher.ENCRYPT_MODE, k);
            byte ptext[]=suffix.getBytes("UTF8");
            byte ctext[]=cp.doFinal(ptext);
            out.writeUTF(ctext.length+"");
            for(int i=0; i<ctext.length; i++){
                out.writeUTF(ctext[i]+"");
            }
            String result = in.readUTF();
            System.out.println("小猴收到胖砾的回答"+result);
            Thread.sleep(500);
        }catch (Exception e){
            System.out.println("胖砾又去吃饭了"+e);
        }
    }
}
